package com.smart.test.spring_el;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * @auther a123
 * @create 2018-08-20 17:21
 * @desc
 */
public class CompilerSample {

    public static void main(String[] args){
        class User{
            public boolean isVipMember(String name){
                if(name == "tom"){
                    return true;
                }else {
                    return false;
                }
            }
        }

        User user = new User();

//        创建解析配置
        SpelParserConfiguration configuration = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,
                CompilerSample.class.getClassLoader());

//        创建分析器
        SpelExpressionParser parser = new SpelExpressionParser(configuration);

//        创建上下文
        EvaluationContext context = new StandardEvaluationContext(user);

//        表达式
        String expression = "isVipMember('tom') && isVipMember('jony')";

//        解析表达式
        SpelExpression spelExpression = parser.parseRaw(expression);

        long start = System.currentTimeMillis();
        System.out.println(spelExpression.getValue(context));
        System.out.println(System.currentTimeMillis() - start);
        start = System.currentTimeMillis();
        System.out.println(spelExpression.getValue(context));
        System.out.println(System.currentTimeMillis() - start);
    }
}
